function createArrOfRect()
{
    var numRect = 5;
    var x = 70;
    for (var rect, i = 0; i < numRect; i++)
    {
        rect = new Rectangle(x, 200, 25, 50, "#00ff00");
        rect.id = "rect" + i;
        rect.counter = 0;
        x += 120;
        g_rectangles.push(rect);
    }
    var x2 = 120;
    for (var rect2, i = 0; i < numRect; i++)
    {
        rect2 = new Rectangle(x2, 150, 25, 50, "#ffd700");
        rect2.id = "rect2" + i;
        rect2.counter = 1;
        x2 += 120;
        g_rectangles2.push(rect2);
    }
    var x3 = 70;
    for (var rect3, i = 0; i < numRect; i++)
    {
        rect3 = new Rectangle(x3, 100, 25, 50, "#ff0000");
        rect3.id = "rect3" + i;
        rect3.counter = 2;
        x3 += 120;
        g_rectangles3.push(rect3);
    }

}

function addStepCircle()
{
    if (!g_firstPress)
    {
        g_circle.x += g_centerCircleX;
        g_circle.y += g_centerCircleY;
    }
}


function platformPositionChange()
{
    if (!stop)
    {
        if ((g_platform.x > zero) && (g_platform.x  < 620))
        {
            speed += ax;
            g_platform.x += speed; 
        }
        else
        {
            speed = zero;
            if (g_platform.x <= zero)
            {
                for ( var i = zero; i <= 2; i += 0.5)
                {
                    g_platform.x += i;
                    g_platform.draw( g_context );
                }
            } 
            if (g_platform.x >= 620)
            {
                for ( var i = 2; i >= zero ; i -= 0.5)
                {
                    g_platform.x -= i;
                    g_platform.draw( g_context );
                }
            } 
        }
    }   
}


function circlePositionChange( )
{       
    if (g_circle.x + g_circle.radius > right) 
    {
        g_circle.x = right - g_circle.radius;
        g_centerCircleX *= reverse;
    }    
    else if (g_circle.x - g_circle.radius < left) 
    {
        g_circle.x = left + g_circle.radius;
        g_centerCircleX *= reverse;
    }
    else if (g_circle.y - g_circle.radius < topOff) 
    {
        g_circle.y = 0 + g_circle.radius;
        g_centerCircleY *= reverse;
    }
    if ((g_circle.y + g_circle.radius >= 548)  &&  (g_circle.x >= g_platform.x) && (g_circle.x <= g_platform.x + g_platform.width) && (g_circle.scaleY > 0.9))
    {        
        for (var x = 1; x >= 0.4; x -= 0.001)
        {
            g_circle.scaleY = x;
            g_circle.draw(g_context);            
        }
    } 
    if ((g_circle.y >= 540) && (g_circle.x >= g_platform.x) && (g_circle.x <= g_platform.x + g_platform.width) && (g_circle.scaleY < 0.9))
    {
        g_circle.y = g_platform.y - g_circle.radius;
        g_centerCircleY *= reverse;

        for  (var x = 0.4; x <= 1; x += 0.001)
        {
            g_circle.scaleY = x;
            g_circle.draw(g_context);    
        }               
    } 
}


function drawRectangles()
{
    var i = g_rectangles.length;
    var k = g_rectangles2.length;
    var l = g_rectangles3.length;
    while (i--)
    {
        drawRect( g_rectangles[i], i, 1 );
    }
    while (k--)
    {
        drawRect( g_rectangles2[k], k, 2 );
    }   
    while (l--)
    {
        drawRect( g_rectangles3[l], l, 3 );
    }
}


function isFinish()
{
    if ((g_circle.y + g_circle.radius >= 570) || ( (g_rectangles.length === 0) && (g_rectangles2.length === 0) && (g_rectangles3.length === 0)))
    {
        stop = true;
        g_circle.x = centerCircleX;
        g_circle.y = centerCircleY;
        g_platform.x = initPlatformX;
        g_platform.y = initPlatformY;
        ax = zero;
        speed = zero;
        g_firstPress = true;
        if ( (g_rectangles.length === 0) && (g_rectangles2.length === 0) && (g_rectangles3.length === 0))
        { 
            g_context.fillStyle = "#00ff00";
            g_context.font = "30px Brush Script Std";
            g_context.fillText("WIN!!!", 330, 300);
        }

    }
}


function typeBricks()
{
    text.value = "Life: " + life + "\nRemove bricks: " + countBricks + "\nScore: " + (countBricks * 50 + score);  
}

function changeColor(rect, disapearRect)
{
    switch (rect.counter )
    {
        case 0:
                disapearRect = true;
                break;
        case 1:
                rect.counter -= 1;
                rect.color = "#00ff00";
                disapearRect = false;
                score += 10;
                break;
        case 2:
                rect.counter -= 1;
                rect.color = "#ffd700";
                disapearRect = false;
                score += 20;
                break;
    }
    return disapearRect
}



function drawRect(rect, i, countArr )
{
    var disapearRect = false, disappearance = false;
    if ( ( ( rect.y + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y <= g_circle.y + g_circle.radius ) && (rect.y >= g_circle.y - g_circle.radius) && ( rect.x + rect.width <= g_circle.x - g_circle.radius ) && (rect.x + rect.width >= g_circle.x - g_circle.radius) ) ||
       ( ( rect.y <= g_circle.y + g_circle.radius ) && ( rect.y <= g_circle.y - g_circle.radius ) && ( rect.y  + rect.height >= g_circle.y + g_circle.radius ) && ( rect.x + rect.width <= g_circle.x - g_circle.radius ) && (rect.x + rect.width >= g_circle.x - g_circle.radius  ) ) || 
       ( ( rect.y <= g_circle.y +  g_circle.radius ) && ( rect.y  + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y + rect.height <= g_circle.y + g_circle.radius ) && ( rect.x + rect.width <= g_circle.x - g_circle.radius ) && ( rect.x + rect.width >= g_circle.x - g_circle.radius ) ) )
    {
        g_centerCircleX *= reverse;
        disappearance = changeColor(rect, disapearRect);                    
    } 
    if ( ( ( rect.y + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y >= g_circle.y - g_circle.radius ) && ( rect.y <= g_circle.y + g_circle.radius ) && ( rect.x >= g_circle.x + g_circle.radius ) && ( rect.x <= g_circle.x + g_circle.radius) ) ||
       ( ( rect.y <= g_circle.y + g_circle.radius ) && ( rect.y <= g_circle.y - g_circle.radius ) && ( rect.y + rect.height >= g_circle.y + g_circle.radius ) && ( rect.x >= g_circle.x + g_circle.radius ) && ( rect.x <= g_circle.x + g_circle.radius ) ) || 
       ( ( rect.y <= g_circle.y +  g_circle.radius ) && ( rect.y + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y + rect.height <= g_circle.y + g_circle.radius ) && ( rect.x >= g_circle.x + g_circle.radius ) && ( rect.x <= g_circle.x + g_circle.radius) ) )
    {
        g_centerCircleX *= reverse;                          
        disappearance = changeColor(rect, disapearRect);
    }

   if ( ( ( rect.x >= g_circle.x - g_circle.radius ) && ( rect.x <= g_circle.x + g_circle.radius ) && ( rect.y <= g_circle.y - g_circle.radius ) && ( rect.y >= g_circle.y + g_circle.radius) ) ||
       ( ( rect.x <= g_circle.x - g_circle.radius ) && ( rect.x + rect.width >= g_circle.x + g_circle.radius ) && ( rect.y <= g_circle.y + g_circle.radius ) && ( rect.y >= g_circle.y - g_circle.radius ) ) || 
       ( ( rect.x + rect.widtht >= g_circle.x - g_circle.radius ) && ( rect.x + rect.width <= g_circle.x + g_circle.radius ) && ( rect.y <= g_circle.y + g_circle.radius ) && ( rect.y >= g_circle.y - g_circle.radius ) ) )
    {
        g_centerCircleY *= reverse;
        disappearance = changeColor(rect, disapearRect);
    }
 
    if ( ( ( rect.x >= g_circle.x - g_circle.radius ) && ( rect.x + rect.width <= g_circle.x + g_circle.radius ) && ( rect.y + rect.height >= g_circle.y - g_circle.radius) ) ||
       ( ( rect.x <= g_circle.x - g_circle.radius ) && ( rect.x + rect.width >= g_circle.x + g_circle.radius ) && ( rect.y + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y + rect.height <= g_circle.y - g_circle.radius )  ) || 
       ( ( rect.x + rect.width >= g_circle.x - g_circle.radius ) && ( rect.x + rect.width <= g_circle.x + g_circle.radius ) && ( rect.y + rect.height >= g_circle.y - g_circle.radius ) && ( rect.y + rect.height <= g_circle.y - g_circle.radius ) ) )
    {
        g_centerCircleY *= reverse;
        disappearance = changeColor(rect, disapearRect);
    }  
    if (disappearance)
    {
        g_context.clearRect( rect.x, rect.y, rect.width, rect.height ); 
        disapearRect = false;
        countBricks = countBricks + 1;
        switch ( countArr )
        {
            case 1:
                    g_rectangles.splice(i, 1);
                    break;
            case 2:
                    g_rectangles2.splice(i, 1);
                    break;   
            case 3:
                    g_rectangles3.splice(i, 1);
                    break; 
        }
    }
    else
    {
        rect.draw(g_context);
    }
    typeBricks();
}


/*function platformPositionChangeLeft( )
{

    if ( g_platform.x - speed >= 0 )
    {
    g_platform.x -= speed;     
    }
    else
    {
        for ( var i = 0; i <= 2; i += 0.5)
        {
            g_platform.x += i;
        }
        
    } 
    drawFrame();    
 
}


function platformPositionChangeRight( )
{
    if ( g_platform.x + speed <= 620 )
    {
        g_platform.x += speed; 
    }
    else
    {
        for ( var i = 2; i >= 0; i -= 0.5)
        {
            g_platform.x -= i;
        }
    }   
    drawFrame();    
 
}
*/
